﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mvvm;
using System.Windows.Controls;
using System.Windows.Input;
using CodeIn.Diagnostics;
using Tomers.WPF.Controls;

namespace CodeIn.Ui.Dock {
	public class PanelViewModel : BaseViewModel {
		private bool isSaved;
		public bool IsSaved {
			get { return isSaved; }
			set { isSaved = value; FirePropertyChanged("IsSaved"); FirePropertyChanged("Title"); }
		}

		private string title;
		public string Title {
			get { return title + (isSaved ? "" : "*"); }
			set { title = value; FirePropertyChanged("Title"); }
		}

		private ContentControl body;
		public ContentControl Body {
			get { return body; }
			set { body = value; FirePropertyChanged("Body"); }
		}

		public PanelViewModel(string title, ContentControl body) {
			Title = title;
			Body = body;
			IsSaved = true;
		}

		private RelayCommand requestCloseCommand;
		public ICommand RequestCloseCommand {
			get {
				if (requestCloseCommand == null)
					requestCloseCommand = new RelayCommand(
						p => {
							var attachedCommandArgs = p as EventCommandParameter<object, System.Windows.RoutedEventArgs>;
							if (attachedCommandArgs != null) {
								var mouseArgs = attachedCommandArgs.EventArgs as System.Windows.Input.MouseButtonEventArgs;
								if (mouseArgs.ChangedButton == MouseButton.Middle)
									RaiseCloseRequested();
							} else {
								RaiseCloseRequested();
							}
						},
						p => true);
				return requestCloseCommand;
			}
		}

		public event EventHandler<CloseRequestedEventArgs> CloseRequested;
		private void RaiseCloseRequested() {
			if (CloseRequested != null)
				CloseRequested(this, new CloseRequestedEventArgs());
		}

		public class CloseRequestedEventArgs : EventArgs {
			public bool Handled { get; set; }
			public CloseRequestedEventArgs() {
				Handled = false;
			}
		}
	}
}
